package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderAdminMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j@RequiredArgsConstructor
public class OrdersTask {
    private final OrderAdminMapper orderAdminMapper;
    @Scheduled(cron = "0 * * * * ?")
    public void  cancelOrder(){
        log.info("------执行自动取消订单任务：开始------");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.plusMinutes(-15L);
        Integer count = orderAdminMapper.updateStatusByPayStatusAndOrderTime(Orders.CANCELLED, Orders.PENDING_PAYMENT,Orders.UN_PAID, localDateTime);
        log.info("------执行自动取消订单任务：结束 | 更新了：{}条------",count);
    }

    @Scheduled(cron = "0 0 1 * * ? ")
    public void finishOrder() {
        log.info("------执行自动完成订单任务：开始------");
        Integer count = orderAdminMapper.updateStatusByStatus(Orders.COMPLETED, Orders.DELIVERY_IN_PROGRESS);
        log.info("------执行自动完成订单任务：结束 | 更新了：{}条------", count);
    }
}
